<template>
  <div class="page2">
    <el-row>
      <el-col class="i_col" :span="12" :offset="12">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item label="股票1">
            <el-select v-model="keyword1" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.value"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="股票2">
            <el-select v-model="keyword2" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.value"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row class="content3">
      <el-col class="c c1" :span="12">
        <div class="echarts echarts1"></div>
      </el-col>
      <el-col class="c c2" :span="12">
        <div class="echarts echarts2"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { stockDiag } from "@/api/page2Api";
const upColor = "#ec0000";
const upBorderColor = "#8A0000";
const downColor = "#00da3c";
const downBorderColor = "#008F28";
// Each item: open，close，lowest，highest
function splitData(rawData) {
  const categoryData = [];
  const values = [];
  for (var i = 0; i < rawData.length; i++) {
    categoryData.push(rawData[i].splice(0, 1)[0]);
    values.push(rawData[i]);
  }
  return {
    categoryData: categoryData,
    values: values,
  };
}

export default {
  name: "page2",
  props: ["selectRangeDate"],
  components: {},
  data() {
    return {
      rData: [],
      keyword1: "上证指数",
      keyword2: "深证成指",
      options: [
        { value: "上证指数" },
        { value: "深证成指" },
        { value: "中小板指" },
        { value: "创业扳指" },
        { value: "沪深300" },
        { value: "上证50" },
      ],
      title1: '',
      title2: '',
      data1: {},
      data0: {}
    };
  },
  methods: {
    search() {
      stockDiag(this.keyword1).then((res) => {
        this.data0 = splitData(res.kline_data);
        this.title1 = res.name;
        this.rChart();
        // console.log(res,'123')
      });
      stockDiag(this.keyword2).then((res) => {
        this.data1 = splitData(res.kline_data);
        this.title2 = res.name;
        this.rChart2();
        // console.log(res,'123')
      });
    },
    rChart() {
      let chart = document.querySelector(".echarts1");
      let myChart = this.$echarts(chart);
      this.option = {
        title: {
          text: this.title1,
          left: 0,
          textStyle: {
            color: "#fff",
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
        },
        legend: {
          data: ["日K", "MA5", "MA10", "MA20", "MA30"],
          textStyle: {
            color: "#FFF",
          },
        },
        grid: {
          left: "10%",
          right: "10%",
          bottom: "15%",
        },
        xAxis: {
          type: "category",
          data: this.data0.categoryData,
          boundaryGap: false,
          axisLine: { onZero: false },
          splitLine: { show: false },
          min: "dataMin",
          max: "dataMax",
          axisLine: {
            lineStyle: {
              color: "#fff",
            },
          },
        },
        yAxis: {
          scale: true,
          splitArea: {
            show: true,
          },
          axisLine: {
            lineStyle: {
              color: "#fff",
            },
          },
        },
        series: [
          {
            name: "日K",
            type: "candlestick",
            data: this.data0.values,
            itemStyle: {
              color: upColor,
              color0: downColor,
              borderColor: upBorderColor,
              borderColor0: downBorderColor,
            },
            markPoint: {
              label: {
                formatter: function (param) {
                  return param != null ? Math.round(param.value) + "" : "";
                },
              },
              data: [
                {
                  name: "Mark",
                  coord: ["2013/5/31", 2300],
                  value: 2300,
                  itemStyle: {
                    color: "rgb(41,60,85)",
                  },
                },
                {
                  name: "highest value",
                  type: "max",
                  valueDim: "highest",
                },
                {
                  name: "lowest value",
                  type: "min",
                  valueDim: "lowest",
                },
                {
                  name: "average value on close",
                  type: "average",
                  valueDim: "close",
                },
              ],
              tooltip: {
                formatter: function (param) {
                  return param.name + "<br>" + (param.data.coord || "");
                },
              },
            },
            markLine: {
              symbol: ["none", "none"],
              data: [
                [
                  {
                    name: "from lowest to highest",
                    type: "min",
                    valueDim: "lowest",
                    symbol: "circle",
                    symbolSize: 10,
                    label: {
                      show: false,
                    },
                    emphasis: {
                      label: {
                        show: false,
                      },
                    },
                  },
                  {
                    type: "max",
                    valueDim: "highest",
                    symbol: "circle",
                    symbolSize: 10,
                    label: {
                      show: false,
                    },
                    emphasis: {
                      label: {
                        show: false,
                      },
                    },
                  },
                ],
                {
                  name: "min line on close",
                  type: "min",
                  valueDim: "close",
                },
                {
                  name: "max line on close",
                  type: "max",
                  valueDim: "close",
                },
              ],
            },
          },
          {
            name: "MA5",
            type: "line",
            data: this.calculateMA(5, this.data0),
            smooth: true,
            lineStyle: {
              opacity: 0.5,
            },
          },
          {
            name: "MA10",
            type: "line",
            data: this.calculateMA(10, this.data0),
            smooth: true,
            lineStyle: {
              opacity: 0.5,
            },
          },
          {
            name: "MA20",
            type: "line",
            data: this.calculateMA(20, this.data0),
            smooth: true,
            lineStyle: {
              opacity: 0.5,
            },
          },
        ],
      };
      myChart.clear();
      myChart.resize();
      myChart.setOption(this.option);
    },
    rChart2() {
      let chart = document.querySelector(".echarts2");
      let myChart = this.$echarts(chart);
      this.option = {
        title: {
          text: this.title2,
          left: 0,
          textStyle: {
            color: "#fff",
          },
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
        },
        legend: {
          data: ["日K", "MA5", "MA10", "MA20", "MA30"],
          textStyle: {
            color: "#FFF",
          },
        },
        grid: {
          left: "10%",
          right: "10%",
          bottom: "15%",
        },
        xAxis: {
          type: "category",
          data: this.data1.categoryData,
          boundaryGap: false,
          axisLine: { onZero: false },
          splitLine: { show: false },
          min: "dataMin",
          max: "dataMax",
          axisLine: {
            lineStyle: {
              color: "#fff",
            },
          },
        },
        yAxis: {
          scale: true,
          splitArea: {
            show: true,
          },
          axisLine: {
            lineStyle: {
              color: "#fff",
            },
          },
        },
        series: [
          {
            name: "日K",
            type: "candlestick",
            data: this.data1.values,
            itemStyle: {
              color: upColor,
              color0: downColor,
              borderColor: upBorderColor,
              borderColor0: downBorderColor,
            },
            markPoint: {
              label: {
                formatter: function (param) {
                  return param != null ? Math.round(param.value) + "" : "";
                },
              },
              data: [
                {
                  name: "Mark",
                  coord: ["2013/5/31", 2300],
                  value: 2300,
                  itemStyle: {
                    color: "rgb(41,60,85)",
                  },
                },
                {
                  name: "highest value",
                  type: "max",
                  valueDim: "highest",
                },
                {
                  name: "lowest value",
                  type: "min",
                  valueDim: "lowest",
                },
                {
                  name: "average value on close",
                  type: "average",
                  valueDim: "close",
                },
              ],
              tooltip: {
                formatter: function (param) {
                  return param.name + "<br>" + (param.data.coord || "");
                },
              },
            },
            markLine: {
              symbol: ["none", "none"],
              data: [
                [
                  {
                    name: "from lowest to highest",
                    type: "min",
                    valueDim: "lowest",
                    symbol: "circle",
                    symbolSize: 10,
                    label: {
                      show: false,
                    },
                    emphasis: {
                      label: {
                        show: false,
                      },
                    },
                  },
                  {
                    type: "max",
                    valueDim: "highest",
                    symbol: "circle",
                    symbolSize: 10,
                    label: {
                      show: false,
                    },
                    emphasis: {
                      label: {
                        show: false,
                      },
                    },
                  },
                ],
                {
                  name: "min line on close",
                  type: "min",
                  valueDim: "close",
                },
                {
                  name: "max line on close",
                  type: "max",
                  valueDim: "close",
                },
              ],
            },
          },
          {
            name: "MA5",
            type: "line",
            data: this.calculateMA(5, this.data1),
            smooth: true,
            lineStyle: {
              opacity: 0.5,
            },
          },
          {
            name: "MA10",
            type: "line",
            data: this.calculateMA(10, this.data1),
            smooth: true,
            lineStyle: {
              opacity: 0.5,
            },
          },
          {
            name: "MA20",
            type: "line",
            data: this.calculateMA(20, this.data1),
            smooth: true,
            lineStyle: {
              opacity: 0.5,
            },
          },
        ],
      };
      myChart.clear();
      myChart.resize();
      myChart.setOption(this.option);
    },
    calculateMA(dayCount,data) {
      var result = [];
      for (var i = 0, len = data.values.length; i < len; i++) {
        if (i < dayCount) {
          result.push("-");
          continue;
        }
        var sum = 0;
        for (var j = 0; j < dayCount; j++) {
          sum += +data.values[i - j][1];
        }
        result.push(sum / dayCount);
      }
      return result;
    },
  },
  mounted() {
    this.search()
  },
};
</script>

<style lang="less" scoped>
.title {
  position: absolute;
  display: inline-block;
  color: #6eddf1;
  background: #14418e;
  left: 50%;
  transform: translate(-50%, -70%);
  font-size: 12px;
  .title-10 {
    // display: inline-block;
    padding: 2px 5px;
    // background: radial-gradient(4.75rem 0.9rem ellipse, rgba(7, 9, 34, .8) 60%, rgb(21, 67, 145));
  }
}
.page2 {
  height: 100%;
  width: 100%;
  padding: 14px 20px 20px;
  background: #000;
  overflow: hidden;

  .content {
    height: 100%;

    .ivu-col {
      height: 100%;
    }

    .circlePie {
      height: 100%;
      padding: 0 0 40px;
      text-align: center;
      position: relative;

      canvas {
        position: absolute;
        left: 50%;
        top: 0;
        transform: translate(-50%, 0);
      }

      #dot {
        background: rgba(0, 0, 0, 0);
      }
    }
    .center_list {
      height: 100%;
    }
    .list {
      height: 48%;

      .left,
      .right {
        background: #0d1341;
      }

      .left,
      .right,
      .center {
        width: 100%;
        height: 95%;
        border: 1px solid #0d2451;
        position: relative;

        #left1,
        #left2,
        #left3,
        #right1,
        #right2,
        #right3,
        #center2 {
          height: 100%;
        }

        .chart-68 {
          width: 100%;
          height: 100%;
          float: left;
        }

        .chart-32 {
          width: 32%;
          height: 100%;
          float: left;
        }
      }
    }
  }

  .bottom-list {
    height: 35%;

    .ivu-col {
      height: 100%;

      .list {
        height: 100%;
        width: 33.3333%;
        // padding-right: 1.5%;
        float: left;

        #bottom_4 {
          padding: 0 20px;
        }

        .bottom {
          width: 100%;
          height: 100%;
          border: 1px solid #0d2451;
          position: relative;
        }
      }

      .right-bottom {
        width: 100%;
        padding-right: 0;

        .bottom1 {
          width: 100%;
          .desc {
            padding: 30px 20px;
            color: #fff;
          }
        }
      }
    }
  }
}
/deep/.el-form .el-form-item .el-form-item__label {
  color: #fff;
}
.content3 {
  height: 75%;
  .c {
    height: 100%;
    .echarts {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
